# FILE: WebEntity.py
# DESC: Base class for types found in a web document
#

from infra.identifiable.Identifiable import Identifiable
from infra.lexical.NamedEntityList import NamedEntityList
from infra.lexical.NoneLexicalFilter import NoneLexicalFilter 
from infra.lexical.NoneContext import NoneContext

class WebEntity(Identifiable):
    def __init__(self, id = -1, name = None, relevanceFactor = 1):
        Identifiable.__init__(self, [id, name, "", ","])
        self.relevanceFactor_ = relevanceFactor
        self.namedEntityList_ = NamedEntityList() 


    def getRelevanceFactor(self):
        return self.relevanceFactor_ 
       
 
    def incrementRelevanceFactor(self, incrementRatio):
    	self.relevanceFactor_ *= incrementRatio
    	


    # REVISIT: enable when more accurate context selection methods will be designed
    # if namedContext:	
    # namedContext.update(self.namedEntityList_)
    def buildNamedEntities(self, lexer, namedContext = NoneContext(), lexFilter = NoneLexicalFilter()):
        #self.namedEntityList_ = lexer.parse(lexFilter.filter(self.utf8Name_), namedContext)
        self.namedEntityList_ = lexer.parse(lexFilter.filter(self), namedContext)


    def setNamedEntities(self, namedEntityList):
        self.namedEntityList_ = namedEntityList 


    def getNamedEntities(self, lexFilters = None):
        if lexFilters:
            for lexFilter in lexFilters:
                self.namedEntityList_.apply(lexFilter)
        return self.namedEntityList_


    def appendNamedEntities(self, namedEntities):
        self.namedEntityList_.extend(namedEntities)



    def hasNamedEntities(self):
        return self.namedEntityList_.size() > 0


    def removeNamedEntity(self, namable):
        for name in namable.getNames():
            self.namedEntityList_.remove(name)


    def toString(self):
        pass


    def toNamedEntity(self, namedEntity):
        namedEntity.attach(self);